package cn.com.dhc.danlu.business.shopcar.popup;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.StringUtil;
import cn.com.dhc.danlu.widget.adapter.BaseRecyclerviewAdapter;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.ToShoppingcartResponse.SellerListBean.MerchListBean.PromotionModelListBean;

class PromotionSelectAdapter extends BaseRecyclerviewAdapter<PromotionSelectAdapter.PromotionSelectViewHolder> {
    private Context context;
    private int promotionIndex;

    private OnSelectListener onSelectListener;

    PromotionSelectAdapter(Context context, int promotionIndex) {
        this.context = context;
        this.promotionIndex = promotionIndex;
    }

    @Override
    public int getItemCount() {
        return super.getItemCount() + 1;
    }

    @Override
    public PromotionSelectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PromotionSelectViewHolder(LayoutInflater.from(context).inflate(R.layout.item_shopcar_promotion_select, parent, false));
    }

    @Override
    public void onBindViewHolder(final PromotionSelectViewHolder holder, final int position) {
        if (position == getItemCount() - 1) {
            // 如果是最后一个
            holder.tvContent.setText(R.string.donot_choose_promotion);
        } else {
            holder.tvContent.setText(StringUtil.hightLight(((PromotionModelListBean) getDatas().get(position)).getPromotionInfo(), ContextCompat.getColor(context, R.color.color_b12525), (int) holder.tvContent.getTextSize()));
        }

        holder.ivMarker.setSelected(promotionIndex == position);
        holder.ivMarker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSelectListener.onSelect(v, position);
                holder.ivMarker.setSelected(true);
                promotionIndex = position;
                notifyDataSetChanged();
            }
        });
    }

    void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    interface OnSelectListener {
        void onSelect(View v, int selectIndex);
    }

    class PromotionSelectViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivMarker;
        private TextView tvContent;

        PromotionSelectViewHolder(View itemView) {
            super(itemView);
            ivMarker = (ImageView) itemView.findViewById(R.id.iv_marker);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}